package com.example.active.sf;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/17 12:47
 */
public class ConsumerPersistent
{
    static ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.0.118:61616");

    public static void main(String[] args)
    {
        try
        {
            System.out.println("消费者2");
            Connection connection = factory.createConnection();
            connection.setClientID("z4");

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //创建目的地
            Topic topic = session.createTopic("topic01");
            //创建消费者
//            MessageConsumer consumer = session.createConsumer(topic);
            TopicSubscriber subscriber = session.createDurableSubscriber(topic, "remark");
            connection.start();
            Message receive = subscriber.receive();
            while (null !=receive){
                TextMessage message = (TextMessage) receive;
                System.out.println("****收到的持久化topic："+message.getText());
                receive = subscriber.receive(3000);
            }

            session.close();
            connection.close();
        } catch (JMSException e)
        {
            e.printStackTrace();
        }
    }
}
